package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var Db *sqlx.DB

type Task struct {
	ProjectName string `db:"project_name"`
	DagId       string `db:"dag_id"`
	TaskId      string `db:"task_id"`
	FuncName    string `db:"func_name"`
}

func init() {
	database, err := sqlx.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test_table")

	if err != nil {
		fmt.Println("open mysql failed", err)
		//defer Db.Close()
		return
	}
	Db = database
}

//airflow_task_func
func main() {
	var task []Task
	//插入
	//r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.com")
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	return
	//}
	//id, err := r.LastInsertId()
	//更新
	//res, err := Db.Exec("update person set username=? where user_id=?", "stu0003", 1)
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	return
	//}
	//row, err := res.RowsAffected()
	//删除
	//res, err := Db.Exec("delete from person where user_id=?", 1)
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	return
	//}
	//row,err := res.RowsAffected()
	//查询
	err := Db.Select(&task, "select project_name, dag_id, task_id, func_name from airflow_task_func")
	if err != nil {
		fmt.Println("exec failed: ", err)
	}
	for _, i := range task {
		fmt.Println(i)
		fmt.Println(i.ProjectName)
	}
	fmt.Println("success : ", task)
}
